---
title: "Untitled"
output: html_document
---
############################################
Title: 3_07_Figure_7_and_8.rmd
Purpose: This code creates Figure 7 and 8.
Last updated: 25-04-2023

Input:
    - Baseline macro data. Latest version: final_agg_macro_data_monthly.csv 

Output:
    - Figure 7 and 8
    
##########################################

0. Setup *Set your file path here*
```{r, results="hide", include=FALSE}
#Clear user defined environment
rm(list=ls())

#Load packages
p_load(dplyr, tidyverse, readxl, ggplot2, tidylog, magrittr,sandwich, lmtest, psych, lm.beta, stargazer, lubridate, vars, urca, mFilter, forecast, tseries)

#options
options(digits=5)
options(stringsAsFactors = FALSE)
options(scipen=999)

#Assign dplyr verbs  
select <- dplyr::select
rename <- dplyr::rename
mutate <- dplyr::mutate
filter <- dplyr::filter
arrange <- dplyr::arrange
distinct <- dplyr::distinct
group_by <- dplyr::group_by
summarise <- dplyr::summarise
list <- base::list

#Define directories
global <- ".../2024_RFS_Replication_TOSUBMIT"

```


#################
1. Load data
################

1.1 Load macro data 
```{r, results="hide", include=FALSE}

#Load
final_agg_macro_data <- read_csv(paste0(global, "/Data/final_agg_macro_data_monthly.csv", ""), 
      col_types = cols(...1 = col_skip(), date = col_date(format = "%Y-%m-%d")))

```

1.2 Select time period
```{r, results="hide", include=FALSE}

#Select sample period
macrofin_m_2000_extd = subset(final_agg_macro_data, date > "1999-11-01" & date < "2023-04-01")  

#Exclude GFC
macrofin_m_2000_extd_excrisis = subset(macrofin_m_2000_extd, date < "2007-12-01" | date > "2009-03-01") 

```


#################
2.VAR
#################

2.1 Figure 7
```{r, results="hide", include=FALSE}

#Data for var
var_df <- macrofin_m_2000_extd %>% 
  dplyr::select(date, agg_loan_spr, med_tom) %>% 
  filter(date >="2002-02-01") %>% 
  mutate(med_tom_3m_ma = ifelse(is.na(med_tom),0,med_tom)) %>% 
  mutate(med_tom = ifelse(is.na(med_tom), 0, med_tom))

#declare time series
loan <- ts(var_df$agg_loan_spr , start=c(2002,02), frequency = 12)
med_tom <- ts(var_df$med_tom , start=c(2002,02), frequency = 12)


#Finding optimal lags
var_ts <- cbind(loan, med_tom)
colnames(var_ts) <- cbind("loan", "med_tom")

lagselect <- VARselect(var_ts, lag.max = 12, type = "const")
lagselect$selection

#Build VAR
mod_1 <- VAR(var_ts, p = 10, type = "const", season = NULL, exog=NULL)
summary(mod_1)


#IRF's
irf_med_tom <- irf(mod_1, impulse = "loan", response = "med_tom", n.ahead = 24, boot=TRUE)
plot(irf_med_tom, main="Response of med_tom to loan shock")

irf_loan <- irf(mod_1, impulse = "med_tom", response = "loan", n.ahead = 24, boot=TRUE)
plot(irf_loan, main="Response of loan to med_tom shock")


#construct in ggplot
a <- do.call(rbind.data.frame, irf_med_tom$irf)
b <- do.call(rbind.data.frame, irf_med_tom$Upper)
c <- do.call(rbind.data.frame, irf_med_tom$Lower)

test <- cbind(a,b,c)
colnames(test) <- c('irf','upper','lower')

test <- as.tibble(test)
test <- test %>% 
  mutate(id = 1:25) %>% 
  select(id, everything())

ggplot(data = test, aes( x = id, y = irf)) +
  geom_line( aes( x = id, y = irf), colour = "dark blue") +
  geom_ribbon( aes(min = lower, max = upper), alpha = 0.1, fill = "red")+
  theme_bw()+
  ggtitle("IRF to loan spread shock") +
  ylab("ToM Responce")+
  xlab("Months") +
  geom_hline( yintercept = 0, linetype = "dashed", color = "red")
  
a3 <- ggplot(data = test, aes( x = id, y = irf)) +
  geom_line( aes( x = id, y = irf), colour = "dark blue") +
  geom_ribbon( aes(min = lower, max = upper), alpha = 0.2, fill = "red")+
  theme_bw()+
  ggtitle("IR of ToM to 1SD shock in loan spread") +
  ylab("ToM response")+
  xlab("Months") +
  geom_hline( yintercept = 0, linetype = "dashed", color = "red") +
  theme(text = element_text(size = 10)) 



a <- do.call(rbind.data.frame, irf_loan$irf)
b <- do.call(rbind.data.frame, irf_loan$Upper)
c <- do.call(rbind.data.frame, irf_loan$Lower)

test <- cbind(a,b,c)
colnames(test) <- c('irf','upper','lower')

test <- as.tibble(test)
test <- test %>% 
  mutate(id = 1:25) %>% 
  select(id, everything())

ggplot(data = test, aes( x = id, y = irf)) +
  geom_line( aes( x = id, y = irf), colour = "dark blue") +
  geom_ribbon( aes(min = lower, max = upper), alpha = 0.1, fill = "red")+
  theme_bw()+
  ggtitle("Cumulative impulse response of loan spread to a 1SD shock in ToM") +
  ylab("Loan spread response")+
  xlab("Months") +
  geom_hline( yintercept = 0, linetype = "dashed", color = "red") 
  
a4 <- ggplot(data = test, aes( x = id, y = irf)) +
  geom_line( aes( x = id, y = irf), colour = "dark blue") +
  geom_ribbon( aes(min = lower, max = upper), alpha = 0.2, fill = "red")+
  theme_bw()+
  ggtitle("IR of loan spread to 1SD shock in ToM") +
  ylab("Loan spread response")+
  xlab("Months") +
  geom_hline( yintercept = 0, linetype = "dashed", color = "red") + 
  theme(text = element_text(size = 10)) 

library(ggpubr)
ggarrange(a3, a4, nrow = 2, ncol = 1)

```

2.2 Figure 8
```{r, results="hide", include=FALSE}

#Data for var
var_df <- macrofin_m_2000_extd %>% 
  select(date, agg_loan_spr, C_B_share_3m_ma) %>% 
  na.omit()

#declare time series
loan <- ts(var_df$agg_loan_spr , start=c(1999,11), frequency = 12)
loan_hy_share <- ts(var_df$C_B_share_3m_ma , start=c(1999,11), frequency = 12)


#Finding optimal lags
var_ts <- cbind(loan, loan_hy_share)
colnames(var_ts) <- cbind("loan", "loan_hy_share")

lagselect <- VARselect(var_ts, lag.max = 12, type = "const")
lagselect$selection

#Build VAR
mod_1 <- VAR(var_ts, p = 4, type = "const", season = NULL, exog=NULL)
summary(mod_1)


#IRF's
irf_loan_hy_share <- irf(mod_1, impulse = "loan", response = "loan_hy_share", n.ahead = 24, boot=TRUE)
plot(irf_loan_hy_share, main="Response of loan_hy_share to loan shock")

irf_loan <- irf(mod_1, impulse = "loan_hy_share", response = "loan", n.ahead = 24, boot=TRUE)
plot(irf_loan, main="Response of loan to loan_hy_share shock")


#construct in ggplot
a <- do.call(rbind.data.frame, irf_loan_hy_share $irf)
b <- do.call(rbind.data.frame, irf_loan_hy_share $Upper)
c <- do.call(rbind.data.frame, irf_loan_hy_share $Lower)

test <- cbind(a,b,c)
colnames(test) <- c('irf','upper','lower')

test <- as.tibble(test)
test <- test %>% 
  mutate(id = 1:25) %>% 
  select(id, everything())

ggplot(data = test, aes( x = id, y = irf)) +
  geom_line( aes( x = id, y = irf), colour = "dark blue") +
  geom_ribbon( aes(min = lower, max = upper), alpha = 0.1, fill = "red")+
  theme_bw()+
  ggtitle("IRF to loan spread shock") +
  ylab("Loan HY share Responce")+
  xlab("Months") +
  geom_hline( yintercept = 0, linetype = "dashed", color = "red")
  
a5 <- ggplot(data = test, aes( x = id, y = irf)) +
  geom_line( aes( x = id, y = irf), colour = "dark blue") +
  geom_ribbon( aes(min = lower, max = upper), alpha = 0.2, fill = "red")+
  theme_bw()+
  ggtitle("IR of HY share to 1SD shock in loan spread") +
  ylab("HY Loan share response")+
  xlab("Months") +
  geom_hline( yintercept = 0, linetype = "dashed", color = "red") +
  theme(text = element_text(size = 10)) 




a <- do.call(rbind.data.frame, irf_loan$irf)
b <- do.call(rbind.data.frame, irf_loan$Upper)
c <- do.call(rbind.data.frame, irf_loan$Lower)

test <- cbind(a,b,c)
colnames(test) <- c('irf','upper','lower')

test <- as.tibble(test)
test <- test %>% 
  mutate(id = 1:25) %>% 
  select(id, everything())

ggplot(data = test, aes( x = id, y = irf)) +
  geom_line( aes( x = id, y = irf), colour = "dark blue") +
  geom_ribbon( aes(min = lower, max = upper), alpha = 0.1, fill = "red")+
  theme_bw()+
  ggtitle("IRF to HY Loan shock") +
  ylab("Loan spread response")+
  xlab("Months") +
  geom_hline( yintercept = 0, linetype = "dashed", color = "red")
  
a6 <- ggplot(data = test, aes( x = id, y = irf)) +
  geom_line( aes( x = id, y = irf), colour = "dark blue") +
  geom_ribbon( aes(min = lower, max = upper), alpha = 0.2, fill = "red")+
  theme_bw()+
  ggtitle("IR of loan spread to 1SD shock in HY share") +
  ylab("Loan spread response")+
  xlab("Months") +
  geom_hline( yintercept = 0, linetype = "dashed", color = "red") +
  theme(text = element_text(size = 10)) 



library(ggpubr)
ggarrange(a5, a6, nrow = 2, ncol = 1)
```
